package mail_remote_control;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;

/**
 *
 * @author Krzysztof Lech (Stiopa)
 */

public class Main {

    public static void main(String[] args) throws NoSuchProviderException, MessagingException, IOException, KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, InvalidKeyException, SignatureException, ClassNotFoundException, java.security.NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, Exception {
        Config conf = new Config(args[0]+"confMail.properties");
        conf.loadProperties();
        Mail mail = new Mail(conf.properties.getProperty("pop3"),conf.properties.getProperty("smtp"),conf.properties.getProperty("usermail"),conf.properties.getProperty("passwordmail"),conf.properties.getProperty("certpath"),conf.properties.getProperty("passwordcert"),conf.properties.getProperty("certalias"));
        mail.countmail = Integer.parseInt(conf.properties.getProperty("mail"));
        mail.getMail();
        conf.saveProperties(Integer.toString(mail.countmail));
        Terminal term = new Terminal(mail.command);
        if (!mail.command.isEmpty()){
            term.commandParse();
            mail.tekst= term.doThisNow();
            mail.sendMail(term.mail, conf.properties.getProperty("from"));
        }else{
            System.out.println("Wszystkie maile zostały już odebrane lub nie ma maili wykonawczych");
        }
    }

}
